package com.tianhua.javs.core.service;

import com.alibaba.fastjson.JSON;
import com.tianhua.javs.client.bean.DomainModelBean;
import com.tianhua.javs.client.bean.JavsScriptMethodBean;
import com.tianhua.javs.client.bean.JavsTranslateContextBean;
import com.tianhua.javs.client.bean.context.TranslateResultContext;
import com.tianhua.javs.client.model.FieldModel;
import com.tianhua.javs.client.model.MethodModel;
import com.tianhua.javs.client.service.JavsScriptEngine;
import com.tianhua.javs.core.factory.TranslateFactory;
import com.tianhua.javs.core.macro.MacroFieldService;
import com.tianhua.javs.core.parse.JavsContentParseService;
import com.tianhua.javs.core.service.translate.TranslateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Description:
 * date: 2022/2/9
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Service
@Slf4j
public class JavsScriptEngineImpl implements JavsScriptEngine {

    @Autowired
    private TranslateFactory translateFactory;

    @Autowired
    private JavsContentParseService javsContentParseService;

    @Autowired
    private TranslateService translateService;

    @Autowired
    private MacroFieldService macroFieldService;

    @Override
    public List<TranslateResultContext> exeParse(DomainModelBean domainModelBean, Map<String, List<String>> javsScriptFileContentMap) {
        List<TranslateResultContext> translateResultContextList = new ArrayList<>();

        if(javsScriptFileContentMap == null || javsScriptFileContentMap.isEmpty()){
            return translateResultContextList;
        }

        JavsTranslateContextBean javsTranslateContextBean = translateFactory.buildTranslateContext(domainModelBean,domainModelBean.getCurrentCompName());

        for (Map.Entry<String,List<String>> entry : javsScriptFileContentMap.entrySet()){
            TranslateResultContext translateResultContext = new TranslateResultContext();
            translateResultContext.setClassName(entry.getKey());
            //解析javs脚本宏定义部分--全局属性解析
            List<FieldModel> fieldModelList = macroFieldService.parseMacroField(entry.getValue(), domainModelBean);
            translateResultContext.addFieldList(fieldModelList);

            //解析javs脚本宏定义部分--类引入
            List<String> packageList = macroFieldService.parsePackageImport(entry.getValue());
            translateResultContext.addPackages(packageList);

            //构建每个脚本文件的文件内容模型
            List<JavsScriptMethodBean> javsScriptMethodBeanList = javsContentParseService.parseContent(javsTranslateContextBean,entry.getKey(),entry.getValue());
            translateService.exeTranslate(javsTranslateContextBean,entry.getKey(),javsScriptMethodBeanList);
            for (JavsScriptMethodBean javsScriptMethodBean : javsScriptMethodBeanList){
                MethodModel methodModel = domainModelBean.searchMethod(javsScriptMethodBean.getClassName(),javsScriptMethodBean.getMethodName(),javsScriptMethodBean.getParamBeanList().size());
                if (methodModel == null){
                    log.warn("javs脚本中的函数定义未匹配.javsScriptMethodBean = "+ JSON.toJSONString(javsScriptMethodBean));
                    continue;

                }
                methodModel.setMethodContent(javsScriptMethodBean.getRefreshMethodContent());
                translateResultContext.addMethodModel(methodModel);
                translateResultContext.addPackages(javsScriptMethodBean.getPackageList());
                translateResultContext.addFieldList(javsScriptMethodBean.getFieldModelList());
            }
            translateResultContextList.add(translateResultContext);
        }

        return translateResultContextList;
    }


}
